<template>
  <div class="main">
    <!--<div class="header_title">验证注册手机</div>-->
    <div class="register-form">
      <!-- 输入手机号-->
      <div class="phone">
        <eagle-input
          v-model="mobile"
          type="number"
          label="手机号"
          placeholder="请输入手机号"
          maxlength="11"
          input-align="right"
        />
        <!-- 验证码 -->
        <eagle-input
          v-model="verifyCode"
          label="验证码"
          clearable
          placeholder="请输入验证码"
        >
          <template #button>
            <van-button
              :disabled="isdisabled"
              size="small"
              type="info"
              @click="_getCode"
              >{{ timer }}</van-button
            >
          </template>
        </eagle-input>
      </div>
      <div class="bottom-btns">
        <eagle-button type="info" class="submit-only" @click="_confirmPhone"
          >提交</eagle-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { SendSms,SendSms2, RegisterVerify,RegisterVerify2 } from "@/api/public";
var wait = 60;
export default {
  components: {},
  data() {
    return {
      code: "",
      mobile: "",
      verifyCode: "",
      qrCode: "",
      timer: "获取验证码",
      isdisabled: false,
    };
  },
  created(){
    console.log(  this.$store.state.OpenID)
  },
  methods: {
    // 获取验证码
    _getCode() {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast("请输入正确的手机号");
        return;
      }
      this.time();
      // 发送请求,用post方式
      const data = {
        mobile: this.mobile,
      };
      if(process.env.VUE_APP_PLANTFORM&&process.env.VUE_APP_PLANTFORM=="GGYJ"){
        data.Sender=process.env.VUE_APP_TITLE
        SendSms2(data)
        .then((res) => {
          if (res.success) {
            console.log("获取成功");
          }
        })
        .catch((err) => {
          console.log("获取失败");
        });
      }else{
        SendSms(data)
        .then((res) => {
          if (res.success) {
            console.log("获取成功");
          }
        })
        .catch((err) => {
          console.log("获取失败");
        });
      }

    },
    // 倒计时
    time() {
      var that = this;
      if (wait == 0) {
        that.code = "";
        wait = 60;
        this.isdisabled = false;
      } else {
        that.code = wait + "s" + "重新发送";
        wait--;
        if (wait == 0) {
          this.timer = "重新发送";
          this.isdisabled = false;
        } else {
          this.timer = wait + "秒后重新发送";
          this.isdisabled = true;
        }
        setTimeout(function () {
          that.time();
        }, 1000);
      }
    },
    // 确认手机号码
    _confirmPhone() {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast("请输入正确的手机号");
        return;
      }
      if (!this.verifyCode) {
        this.$toast("请输入验证码");
        return;
      }
      const data = {
        mobile: +this.mobile,
        verifyCode: +this.verifyCode,
      };
      if(process.env.VUE_APP_PLANTFORM&&process.env.VUE_APP_PLANTFORM=="GGYJ"){
        RegisterVerify2(data)
        .then((res) => {
          if (!res.success) {
            this.$toast(res.errorText);
            return;
          }
          //判断是否有注册手机
          if (this.qrCode) {
            if (this.dCode) {
              //已经有CompanyCode
              if (this.dType) {
                if (this.dType == "Company") {
                }
              } else {
                setTimeout(() => {}, 1500);
              }
            } else {
              this.$router.push("/login");
            }
          } else if (this.dType == "sgfCompany") {
            this.$router.push({
              path: "/registerIDCard",
              query: {
                CompanyCode: this.CompanyCode,
                mobile: this.mobile,
              },
            });
          } else if (process.env.VUE_APP_DEFAULT_PAGE == "/palogin") {
            setTimeout(() => {
              this.$router.push({
                path: "/PARegister",
                query: { mobile: this.mobile },
              });
            }, 1500);
          } else {
            //判断是否扫码验证过公司
            this.$axios.get("/Public/HasRegisterCompany").then((res) => {
              if (res.data == "1") {
                this.$router.push({
                  path: "/registerInfo",
                  query: {
                    mobile: this.mobile,
                  },
                });
              } else {
                this.$router.push({
                  path: "/scan",
                  query: {
                    mobile: this.mobile,
                  },
                });
              }
            });
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
      }else{
        RegisterVerify(data)
        .then((res) => {
          if (!res.success) {
            this.$toast(res.errorText);
            return;
          }
          //判断是否有注册手机
          if (this.qrCode) {
            if (this.dCode) {
              //已经有CompanyCode
              if (this.dType) {
                if (this.dType == "Company") {
                }
              } else {
                setTimeout(() => {}, 1500);
              }
            } else {
              this.$router.push("/login");
            }
          } else if (this.dType == "sgfCompany") {
            this.$router.push({
              path: "/registerIDCard",
              query: {
                CompanyCode: this.CompanyCode,
                mobile: this.mobile,
              },
            });
          } else if (process.env.VUE_APP_DEFAULT_PAGE == "/palogin") {
            setTimeout(() => {
              this.$router.push({
                path: "/PARegister",
                query: { mobile: this.mobile },
              });
            }, 1500);
          } else {
            //判断是否扫码验证过公司
            this.$axios.get("/Public/HasRegisterCompany").then((res) => {
              if (res.data == "1") {
                this.$router.push({
                  path: "/registerInfo",
                  query: {
                    mobile: this.mobile,
                  },
                });
              } else {
                this.$router.push({
                  path: "/scan",
                  query: {
                    mobile: this.mobile,
                  },
                });
              }
            });
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
      }
  
    },
  },
};
</script>

<style lang="less" scoped>
.phone {
  position: relative;

  .code {
    position: absolute;
    right: 20px;
    top: 20px;
    font-size: 12px;
  }
}
</style>
